package com.evil.common.application.enums;

import cn.hutool.core.bean.BeanUtil;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 货币单位
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum CurrencyCodeEnum {

    /**
     * 货币单位
     */
    RMB("RMB", "人民币", "￥"),
    HKD("HKD", "港币", "＄"),
    JPY("JPY", "日元", "¥"),
    USD("USD", "美元", "$"),
    GBP("GBP", "英镑", "£"),
    EUR("EUR", "欧元", "€"),
    CHF("CHF", "瑞士法郎", "₣"),
    FRF("FRF", "法国法郎", "₣"),
    DEM("DEM", "德国马克", "ℳ"),
    THB("THB", "泰铢", "฿"),
    KRW("KRW", "韩国元", "₩"),
    SGD("SGD", "新加坡元", "＄"),
    CAD("CAD", "加拿大元", "¤"),
    AUD("AUD", "澳大利亚元", "$"),
    ATS("ATS", "奥地利先令", "¤"),
    FIM("FIM", "芬兰马克", "¤"),
    BEF("BEF", "比利时法郎", "₣"),
    IEP("IEP", "爱尔兰镑", "¤"),
    ITL("ITL", "意大利里拉", "₤"),
    LUF("LUF", "卢森堡法郎", "¤"),
    NLG("NLG", "荷兰盾", "¤"),
    PTE("PTE", "葡萄牙埃斯库多", "¤"),
    ESP("ESP", "西班牙比塞塔", "¤"),
    IDR("IDR", "印尼盾", "¤"),
    MYR("MYR", "马来西亚林吉特", "¤"),
    NZD("NZD", "新西兰元", "¤"),
    PHP("PHP", "菲律宾比索", "¤"),
    SUR("SUR", "俄罗斯卢布", "руб"),
    ;
    @Getter
    private final String currencyCode;
    @Getter
    private final String currencyCodeCn;
    @Getter
    private final String currencyCodeSymbol;

    public static Optional<CurrencyCodeEnum> findByCode(String code) {
        return Arrays.stream(values()).filter(e -> e.currencyCode.equals(code)).findFirst();
    }

    public static CurrencyCodeEnum isExistByCode(String code) {
        return CurrencyCodeEnum.findByCode(code).orElseThrow(()
                -> new BusinessException(RCodeEnum.ERROR_PARAMETER, "货币单位参数错误"));
    }

    public static List<Map<String, Object>> codesMap() {
        return Arrays.stream(values()).map(BeanUtil::beanToMap).collect(Collectors.toList());
    }
}
